#!/usr/bin/python
# -*- coding: UTF-8 -*-
import requests
import gevent
from gevent import monkey

max_thread_num = 10
max_request_timeout = 2

proxy_data_file_name = "proxy_list.dat"
proxy_test_url = "http://www.baid.com"
proxy_avilable_data_file_name = 'avilable_proxy_list.dat'

def dat_file_open(filename):
	try:
		fp = open(filename, "r")
		print("%s open success" %(filename))
		return fp

	except IOError:
		print("%s open failed" %(filename))

file = dat_file_open(proxy_data_file_name)

def check_proxy_line(proxy_data):
	proxy_setting = {
		'http': proxy_data, 
		'https': proxy_data
	}
	try:
		r = requests.get(proxy_test_url, proxies=proxy_setting, timeout=max_request_timeout)
		print(r.status_code)
		if r.status_code == 200:
			with open(proxy_avilable_data_file_name, 'a+') as f:
				f.write(proxy_data + '\r')

	except Exception as e:
		print(proxy_data + " failed")

if file:
	readed_line_num = 0
	task_list = []

	while True:
		file_line = file.readline()
		if not file_line:
			break;
		else:
			if '#' == file_line[0:1]:
				continue
			else:
				file_line = file_line.replace('\n','').replace('\r','')
				if len(file_line.split(':')) != 2:
					continue

				check_proxy_line(file_line)

	# 			if readed_line_num < max_thread_num:
	# 				# readed_line.append(file_line)
	# 				task = gevent.spawn(check_proxy_line, file_line)
	# 				task_list.append(task)

	# 				# readed_line_num = readed_line_num + 1
	# 			else:
	# 				readed_line_num = 0
	# 				# gevent.joinall(task_list)
	# 				task_list = []

	# gevent.joinall(task_list)

	file.close()

					
					
